﻿using System.Xml;
using System.Xml.Linq;
using Xunit;
using Xunit.Should;

namespace Adjunct.Framework.Nuspec
{
	public class NuspecLibReferenceSpec
	{
		private const string ValidXml = "<reference file='xunit.dll' />";
		private readonly NuspecLibReference _xunitReference = new NuspecLibReference("xunit.dll");

		[Fact]
		public void FromNullXElement()
		{
			XElement element = null;
			((NuspecLibReference)element).ShouldBeNull();
		}

		[Fact]
		public void FromValidXElement()
		{
			((NuspecLibReference)XElement.Parse(ValidXml)).ShouldEqual(_xunitReference);
		}

		[Fact]
		public void MissingFilenameToXElementThrows()
		{
			NuspecLibReference reference = new NuspecLibReference();
			XmlException exception = Assert.Throws<XmlException>(() => (XElement)reference);
			exception.Message.ShouldEqual("Can not create attribute 'file', the value was null.");
		}

		[Fact]
		public void NullToXElement()
		{
			NuspecLibReference reference = null;
			((XElement)reference).ShouldBeNull();
		}

		[Fact]
		public void ValidToXElement()
		{
			((XElement)_xunitReference).ShouldEqual(ValidXml);
		}
	}
}